package com.luck.easyfoodbatch.mvp.mine;

import android.content.Context;
import android.support.annotation.Nullable;

import com.luck.easyfoodbatch.callback.JsonCallback;
import com.luck.easyfoodbatch.mvp.model.PointModel;
import com.luck.easyfoodbatch.mvp.model.PointsModel;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.Tools;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.model.HttpParams;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.mvp.mine
 * email：893855882@qq.com
 * data：16/10/19
 */
public class PointChangePresenter implements PointChangeModel {
    private final String TAG = getClass().getSimpleName();
    private PointChangeView pointChangeView;
    private Context mContext;

    public PointChangePresenter(PointChangeView pointChangeView, Context mContext) {
        this.pointChangeView = pointChangeView;
        this.mContext = mContext;
    }


    @Override
    public void getPointChangeList(int page, String vids) {
        HttpParams params = new HttpParams();
        params.put("page", page + "");
        if (!Tools.isNull(vids) && !vids.equals("0")) {
            params.put("vids", vids);
        } else {
            params.put("vids", "");
        }
        OkHttpUtils.post(HttpUtil.POINT_CHANGE)
                .tag(this)
                .cacheMode(CacheMode.DEFAULT)
                .cacheKey(TAG)
                .params(params)
                .execute(new JsonCallback(mContext, PointModel.class) {

                    @Override
                    public void onResponse(boolean isFromCache, Object data, Request request, @Nullable Response response) {
                        pointChangeView.pointChangeSuccess((PointModel) data);
                        pointChangeView.hideLoading();
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                        pointChangeView.hideLoading();
                        pointChangeView.httpStatusError();
                    }
                });
    }
}
